/*
 * Copyright (c) 2007 Alexey Vatchenko <av@bsdua.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <errno.h>
#include <string.h>

#include "defs.h"

/*
 * Implementation of CP1251. Table information is taken from
 * http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1251.TXT
 */

static const u_short cp12512wc[128] = {
	0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021,
	0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F,
	0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
	0x0000, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F,
	0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7,
	0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407,
	0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7,
	0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457,
	0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417,
	0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
	0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
	0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
	0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437,
	0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
	0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447,
	0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F
};

static const u_short wc2cp1251_00[] = {	/* 0x00a0 - 0x00bb */
	0xa0, 0x00, 0x00, 0x00, 0xa4, 0x00, 0xa6, 0xa7,
	0x00, 0xa9, 0x00, 0xab, 0xac, 0xad, 0xae, 0x00,
	0xb0, 0xb1, 0x00, 0x00, 0x00, 0xb5, 0xb6, 0xb7,
	0x00, 0x00, 0x00, 0xbb
};

static const u_short wc2cp1251_04[] = {	/* 0x0401 - 0x0491 */
	0xa8, 0x80, 0x81, 0xaa, 0xbd, 0xb2, 0xaf, 0xa3,
	0x8a, 0x8c, 0x8e, 0x8d, 0x00, 0xa1, 0x8f, 0xc0,
	0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
	0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
	0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
	0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0,
	0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
	0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0,
	0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
	0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0x00,
	0xb8, 0x90, 0x83, 0xba, 0xbe, 0xb3, 0xbf, 0xbc,
	0x9a, 0x9c, 0x9e, 0x9d, 0x00, 0xa2, 0x9f, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa5,
	0xb4
};

static const u_short wc2cp1251_20[] = {	/* 0x2013 - 0x203a */
	0x96, 0x97, 0x00, 0x00, 0x00, 0x91, 0x92, 0x82,
	0x00, 0x93, 0x94, 0x84, 0x00, 0x86, 0x87, 0x95,
	0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x89, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8b, 0x9b
};

int	cp1251_mbtowc(struct conv_state *cs, const char *in, size_t insize,
	    WIDECHAR *out, size_t *inused);
int	cp1251_wctomb(struct conv_state *cs, WIDECHAR in, char *out,
	    size_t outsize, size_t *outused);

/* ARGSUSED */
int
cp1251_mbtowc(struct conv_state *cs, const char *in, size_t insize,
    WIDECHAR *out, size_t *inused)
{
	u_char ch;

	ch = *(u_char *)in;
	if (ch < 0x80)
		*out = ch;
	else {
		if (cp12512wc[ch - 0x80] == 0x0000)
			return (EILSEQ);
		*out = cp12512wc[ch - 0x80];
	}

	*inused = 1;
	return (0);
}

int
cp1251_wctomb(struct conv_state *cs, WIDECHAR in, char *out, size_t outsize,
    size_t *outused)
{
	u_char ch;

	if (outsize < 1)
		return (E2BIG);

	if (in < 0x80)
		*out = (char)in;
	else {
		ch = 0;
		if (in >= 0x00a0 && in < 0x00a0 + sizeof(wc2cp1251_00))
			ch = wc2cp1251_00[in - 0x00a0];
		else if (in >= 0x0401 && in < 0x0401 + sizeof(wc2cp1251_04))
			ch = wc2cp1251_04[in - 0x0401];
		else if (in >= 0x2013 && in < 0x2013 + sizeof(wc2cp1251_20))
			ch = wc2cp1251_20[in - 0x2013];
		else if (in == 0x20ac)	/* #EURO SIGN */
			ch = 0x88;
		else if (in == 0x2116)	/* #NUMERO SIGN */
			ch = 0xb9;
		else if (in == 0x2122)	/* #TRADE MARD SIGN */
			ch = 0x99;

		if (ch == 0)
			return (EILSEQ);
		*(u_char *)out = ch;
	}
	*outused = 1;
	return (0);
}
